package com.anhuanjia.tools.eao.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.springframework.util.StringUtils;

import com.anhuanjia.bean.MhBranch;
import com.anhuanjia.bean.MhResource;
import com.anhuanjia.tools.eao.bean.AhjMemberRemark;
import com.anhuanjia.tools.eao.bean.EaoEngineerMember;
import com.anhuanjia.tools.eao.bean.EaoIsEngineer;
import com.anhuanjia.tools.eao.bean.EaoPosition;
import com.anhuanjia.tools.eao.bean.EaoProjectInfo;
import com.anhuanjia.tools.eao.bean.EaoProjectStaff;
import com.anhuanjia.tools.eao.beanvo.EaoBranchVo;
import com.efastsoft.bean.CrmPeople;
import com.efastsoft.bean.EcMember;
import com.efastsoft.framework.Member;
import com.efastsoft.framework.UIFacade;
import com.efastsoft.framework.annotation.Alias;
import com.efastsoft.framework.annotation.Priv;
import com.efastsoft.framework.annotation.Priv.LoginType;
import com.efastsoft.framework.collection.Mapx;
import com.efastsoft.framework.data.DataRow;
import com.efastsoft.framework.data.DataTable;
import com.efastsoft.framework.data.Q;
import com.efastsoft.framework.data.Transaction;
import com.efastsoft.framework.utility.StringUtil;
import com.efastsoft.member.bl.MemberBL;
import com.efastsoft.member.item.CommonMemberType;

/**
 * 资本资料
 * 
 * @author
 *
 */
@Alias(value = "basicInformation")
public class BasicInformationUI extends UIFacade {
	/**
	 * 删除人员
	 */
	@Priv(loginType = LoginType.Member)
	public void deleteInformation() {
		String memberId = $V("MemberId");
		Transaction trans = new Transaction();
		String name = new Q(" select RealName from Ec_Member where Id = ? ", memberId).executeString();
		String deleteStr = "删除&nbsp;&nbsp" + name + "&nbsp;&nbsp后将影响到某些项目流程，以下环节重新进行人员分配后才能删除此人：" + name + "</br>";
		String deleteStr5 = name + "为机构唯一档案员，删除&nbsp;&nbsp" + name + "&nbsp;&nbsp后将影响到某些项目流程，分配新的档案员后才能删除此人：" + name
				+ "</br>";
		if (StringUtil.isNotEmpty(memberId)) {

//			档案员
			DataTable position = new Q(
					" select Id , ParentId from eao_Position where Type = ? and Id = (select IsEngineer from eao_is_engineer where Member = ?) ",
					Member.getCompany(), memberId).fetch();
			if (position.getRowCount() != 0 && (position.getInt(0, 0) == 7 || position.getInt(0, 1) == 7)) {
				Integer positionId = new Q(" select Id from eao_Position where Type = ? and ParentId = 7 ",
						Member.getCompany()).executeInt();
				if (positionId == 0) {
					int count = new Q(
							" select count(*) from eao_is_engineer as ei join mh_resource as mr on ei.Member = mr.Member and mr.Company = ? where IsEngineer = 7 ",
							Member.getCompany()).executeInt();
					DataTable dataTable = new Q(" select * from eao_project_info where PerjectType <> 16").fetch();
					if (count == 1 && dataTable.getRowCount() != 0) {
						f(deleteStr5);
						return;
					}
				} else {
					int count = new Q(
							" select count(*) from eao_is_engineer as ei join mh_resource as mr on ei.Member = mr.Member and mr.Company = ? where IsEngineer = ? ",
							Member.getCompany(), positionId).executeInt();
					DataTable dataTable = new Q(" select * from eao_project_info where PerjectType <> 16").fetch();
					if (count == 1 && dataTable.getRowCount() != 0) {
						f(deleteStr5);
						return;
					}
				}
			}

			// 项目组成员/负责人
			DataTable Table = new Q(
					" select ProjectId from eao_project_staff where 1 = 1 and ReportChapter is null and StaffName = ?",
					memberId).fetch();
			List<String> listStr = new ArrayList<>();
			List<String> listProjectId = new ArrayList<>();
			if (Table.getRowCount() != 0) {
				for (DataRow dataRow : Table) {
					listStr.add(dataRow.getString(0));
				}
			}

			DataTable projectDt = null;
			Q leader = new Q(
					" select * from eao_project_info where (PerjectType <> 16) and PerjectLeader = " + memberId + " ");
			if (Table.getRowCount() > 0) {
				Q q = new Q(" select * from eao_project_info where (PerjectType <> 16 or PerjectType > 2) ");
				q.and().in("id ", listStr);
				projectDt = q.fetch();
			}
			String staffProject = null;
			if (projectDt != null && projectDt.getRowCount() != 0) {
				for (DataRow dataRow : projectDt) {
					EaoProjectInfo projectInfo = new EaoProjectInfo();
					projectInfo.setValue(dataRow);
					listProjectId.add(projectInfo.getId());
					if (StringUtils.isEmpty(staffProject)) {
						staffProject = projectInfo.getProjectName();
					} else {
						staffProject = staffProject + "," + projectInfo.getProjectName();
					}
				}
			}
			DataTable projectleader = leader.fetch();
			String leaderProject = null;
			if (projectDt != null && projectDt.getRowCount() != 0) {
				for (DataRow dataRow : projectleader) {
					EaoProjectInfo projectInfo = new EaoProjectInfo();
					projectInfo.setValue(dataRow);
					listProjectId.add(projectInfo.getId());
					if (StringUtils.isEmpty(leaderProject)) {
						leaderProject = projectInfo.getProjectName();
					} else {
						leaderProject = staffProject + "," + projectInfo.getProjectName();
					}
				}
			}

//			项目审核人
			DataTable auditer = new Q(
					" select pi.ProjectName , pi.PerjectType , a.AuditType from eao_project_info as pi join eao_audit as a on a.ProjectId = pi.Id where Auditor = ? ",
					memberId).fetch();
			String auditProject = null;
			for (DataRow dataRow : auditer) {
				String auditType = dataRow.getString("AuditType");
				int perjectType = dataRow.getInt("PerjectType");
				int auditT = 0;
				switch (auditType) {
				case "一审":
					auditT = 6;
					break;
				case "二审":
					auditT = 7;
					break;
				case "初审":
					auditT = 8;
					break;
				case "复审":
					auditT = 9;
					break;
				case "终审":
					auditT = 10;
					break;
				}
				if (auditT >= perjectType) {
					if (StringUtils.isEmpty(auditProject)) {
						auditProject = dataRow.getString("ProjectName");
					} else {
						auditProject = auditProject + "," + dataRow.getString("ProjectName");
					}
				}
			}

			String deleteStr1 = "项目负责人：" + leaderProject + "</br>";
			String deleteStr2 = "项目组成员：" + staffProject + "</br>";
			String deleteStr3 = "审核人员：" + auditProject + "</br>";
//			String deleteStr4 = "<p>章节分配：（提示项目名称，前后项目名称相隔两个空格，仅其参加了章节分配才会有提示。项目已入库不在进行提示。）</p>";
//			String deleteStr5 = "<p>档案员： <p>";
			if (!StringUtils.isEmpty(staffProject) && !StringUtils.isEmpty(leaderProject)
					&& !StringUtils.isEmpty(auditProject)) {
				f(deleteStr + deleteStr1 + deleteStr2 + deleteStr3);
				return;
			} else if (!StringUtils.isEmpty(staffProject) && !StringUtils.isEmpty(auditProject)) {
				f(deleteStr + deleteStr2 + deleteStr3);
				return;
			} else if (!StringUtils.isEmpty(auditProject)) {
				f(deleteStr + deleteStr3);
				return;
			} else if (!StringUtils.isEmpty(staffProject)) {
				f(deleteStr + deleteStr2);
				return;
			}

//			删除
			Q deletePeople = new Q(" DELETE FROM Mh_Resource WHERE Member = ? and Company = ?", memberId,
					Member.getCompany());
			trans.add(deletePeople);
			// 关联删除工程师库
			Q dt = new Q("select * from eao_engineer_member where Company = ?", Member.getCompany());
			dt.and().in(" Member ", memberId);
			DataTable dataTable = dt.fetch();
			if (dataTable.getRowCount() != 0) {
				for (DataRow dataRow : dataTable) {
					EaoEngineerMember engineer = new EaoEngineerMember();
					engineer.setValue(dataRow);
					trans.delete(engineer);
				}

			}
			Q dt1 = new Q("select * from eao_is_engineer where ");
			dt1.in(" Member ", memberId);
			DataTable table = dt1.fetch();
			if (table.getRowCount() != 0) {
				for (DataRow dataRow : table) {
					EaoIsEngineer engineer = new EaoIsEngineer();
					engineer.setValue(dataRow);
					trans.delete(engineer);
				}

			}
			Q dt2 = new Q("select * from eao_project_staff where StaffName = ? ", memberId);
			dt2.and().in("ProjectId", listProjectId);
			DataTable projectStaff = dt2.fetch();
			if (projectStaff.getRowCount() != 0) {
				for (DataRow dataRow : projectStaff) {
					EaoProjectStaff entity = new EaoProjectStaff();
					entity.setValue(dataRow);
					trans.delete(entity);
				}
			}
			if (trans.commit()) {
				s("删除成功");
			} else {
				f("删除失败");
			}
		} else {
			f("参数不能为空");
		}

	}

	/**
	 * 修改人员管理
	 */
	@Priv(loginType = LoginType.Member)
	public void updateInformation() {
		String memberId = $V("MemberId");
		String positionId = $V("PositionId");
		String branchInnerCode = $V("BranchInnerCode");
		String realName = $V("RealName");
		String sex = $V("Sex");
		String parentId = null;
		String companyType = null;
		if (StringUtil.isNotEmpty(positionId) && StringUtil.isNotEmpty(branchInnerCode)) {
			parentId = new Q(" select ParentId from eao_Position where Id = ?", positionId).executeString();
			companyType = new Q(
					" select CompanyType from Mh_Branch where BranchInnerCode = (select ParentInnerCode from Mh_Branch where BranchInnerCode = ? and Company = ?) ",
					branchInnerCode, Member.getCompany()).executeString();
		} else if (StringUtil.isNotEmpty(positionId) && StringUtils.isEmpty(branchInnerCode)) {
			parentId = new Q(" select parentId from eao_Position where id = ?", positionId).executeString();
			companyType = new Q(
					" select CompanyType from Mh_Branch where BranchInnerCode = (select ParentInnerCode from Mh_Branch where BranchInnerCode = (select BranchInnerCode from Mh_Resource where Member = ? )  and Company = ?) ",
					memberId, Member.getCompany()).executeString();
		} else if (StringUtils.isEmpty(positionId) && StringUtil.isNotEmpty(branchInnerCode)) {
			positionId = new Q(" select IsEngineer from eao_is_engineer where Member = ?", memberId).executeString();
			parentId = new Q(" select ParentId from eao_position where Id = ?", positionId).executeString();
			companyType = new Q(
					" select CompanyType from mh_branch where BranchInnerCode = (select ParentInnerCode from Mh_Branch where BranchInnerCode = ?  and Company = ?) ",
					branchInnerCode, Member.getCompany()).executeString();
		}
		if (StringUtil.isNotEmpty(parentId)) {
			if ("0".equals(companyType) && "3".equals(parentId)) {
				f("匹配有误，无法保存");
				return;
			} else if ("1".equals(companyType)
					&& ("1".equals(parentId) || "2".equals(parentId) || "7".equals(parentId))) {
				f("匹配有误，无法保存");
				return;
			}
		} else {
			if ("0".equals(companyType) && "3".equals(positionId)) {
				f("匹配有误，无法保存");
				return;
			} else if ("1".equals(companyType)
					&& ("1".equals(positionId) || "2".equals(positionId) || "7".equals(positionId))) {
				f("匹配有误，无法保存");
				return;
			}
		}
		Transaction trans = new Transaction();
		if (StringUtil.isNotEmpty(memberId)) {
			if (StringUtil.isNotEmpty(positionId)) {
				DataTable dataTable = new Q(" select * from eao_is_engineer where Member = ?", memberId).fetch();
				if (dataTable.getRowCount() != 0) {
					Q updatePosition = new Q(" UPDATE eao_is_engineer SET IsEngineer = ? WHERE  Member = ? ",
							positionId, memberId);
					trans.add(updatePosition);
					if (trans.commit()) {
						s("职位修改成功");
					} else {
						f("职位修改失败");
					}
				} else {
					EaoIsEngineer engineer = new EaoIsEngineer();
					engineer.setMember(Integer.valueOf(memberId));
					engineer.setIsEngineer(positionId);
					trans.insert(engineer);
					if (trans.commit()) {
						s("职位修改成功");
					} else {
						f("职位修改失败");
					}
				}
			}
			if (StringUtil.isNotEmpty(branchInnerCode)) {
				Q updateResource = new Q(" UPDATE Mh_Resource SET BranchInnerCode= ? , FullName = ? WHERE  Member = ? ",
						branchInnerCode, realName, memberId);
				trans.add(updateResource);
				if (trans.commit()) {
					s("公司/部门修改成功");
				} else {
					f("公司/部门修改失败");
				}
			}
			if (StringUtil.isNotEmpty(realName) || StringUtil.isNotEmpty(sex)) {
				DataTable dataTable = new Q(" select * from eao_engineer_member where Member = ? ", memberId).fetch();
				if (dataTable.getRowCount() != 0) {
					EaoEngineerMember engineerMember = new EaoEngineerMember();
					engineerMember.setValue(dataTable.get(0));
					engineerMember.setUserName(realName);
					trans.update(engineerMember);
				}
				Q updateSex = new Q(" UPDATE Ec_Member SET ");
				Q updateGender = new Q(" UPDATE crm_people SET ");
				if (StringUtil.isNotEmpty(realName) && StringUtil.isNotEmpty(sex)) {
					updateSex.append(" Gender=? , RealName=? ", sex, realName);
					updateGender.append(" Gender=? , FirstName = ? , LastName = ? ,FullName=? ", sex,
							realName.substring(0, 1), realName.substring(1), realName);
				} else if (StringUtil.isNotEmpty(sex)) {
					updateSex.append(" Gender=? ", sex);
					updateGender.append(" Gender=? ", sex);
				} else if (StringUtil.isNotEmpty(realName)) {
					updateSex.append(" RealName=? ", realName);
					updateGender.append(" FirstName = ? , LastName = ? ,FullName=? ", realName.substring(0, 1),
							realName.substring(1), realName);
				}
				updateSex.append(" WHERE  Id = ? ", memberId);
				updateGender.append(" WHERE  Id = ? ", memberId);
				trans.add(updateSex);
				if (trans.commit()) {
					s("信息修改成功");
				} else {
					f("信息修改失败");
				}
			}
		} else {
			f("id不能为空");
		}

	}

	/**
	 * 职位删除
	 */
	@Priv(loginType = LoginType.Member)
	public void deletePosation() {
		String PositionId = $V("PositionId");
		long companyId = 0;
		if ("Ent".equals(Member.getType())) {
			companyId = Member.getUID();
		} else {
			companyId = Member.getCompany();
		}
		Transaction trans = new Transaction();
		if (StringUtil.isNotEmpty(PositionId)) {
			Q deletePosition = new Q(" DELETE FROM eao_Position WHERE ").in("Id", PositionId);
			int newPositionId = Integer.parseInt(PositionId);
			if (newPositionId > 8 || newPositionId == 4) {
				trans.add(deletePosition);
				// 职称修改 人员联动修改
				List<String> list = new ArrayList<>();
				String parentId = new Q(" select ParentId from eao_Position where Id = ?", PositionId).executeString();
				DataTable dataTable = new Q(" select Member from Mh_Resource where Company = ?", companyId).fetch();
				if (dataTable.getRowCount() != 0) {
					for (DataRow dataRow : dataTable) {
						list.add(dataRow.getString(0));
					}
					Q q = new Q(" update eao_is_engineer set IsEngineer = ? where IsEngineer = ?", parentId,
							PositionId);
					q.and().in("member", list);
					trans.add(q);
				}
				if (trans.commit()) {
					s("删除成功");
				} else {
					f("删除失败");
				}
			} else {
				f("您无权删除");
			}
		} else {
			f("PositionId为空");
		}

	}

	/**
	 * 职位添加/修改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveOrUpdate() {
		String id = $V("Id");
		String position = $V("Position");
		String parentId = $V("ParentId");
		long companyId = 0;
		if ("Ent".equals(Member.getType())) {
			companyId = Member.getUID();
		} else {
			companyId = Member.getCompany();
		}
		if (StringUtil.isNotEmpty(id)) {
			DataTable branch1 = new Q(" select * from eao_Position where Id = ?", id).fetch();
			if (branch1.getRowCount() != 0) {
				Transaction trans = new Transaction();
				Q updatePosition = new Q(" UPDATE eao_Position SET  ");
				if (StringUtil.isNotEmpty(position)) {
					updatePosition.append("Position = ? ");
					if (StringUtil.isNotEmpty(parentId)) {
						updatePosition.append(",");
					}
				}
				if (StringUtil.isNotEmpty(parentId)) {
					updatePosition.append("ParentId = ? ");
				}
				if (StringUtil.isNotEmpty(position)) {
					updatePosition.append(" ", position);
				}
				if (StringUtil.isNotEmpty(parentId)) {
					updatePosition.append(" ", parentId);
				}
				updatePosition.append(" where Id = ? ", id);
				trans.add(updatePosition);
				if (trans.commit()) {
					s("修改成功");
				} else {
					f("修改失败");
				}
			}
		} else {
			if (StringUtil.isNotEmpty(position)) {
				DataTable insertPosition = new Q(" select * from eao_Position where Position = ? and Type = ?", position, Member.getCompany()).fetch();
				if (insertPosition.getRowCount() != 0) {
					f(" 职位名称不能重复，请重新填写！");
					return;
				}
			} else {
				f("请填写职位名称！");
				return;
			}
			Transaction transaction = new Transaction();
			DataTable insertPosition = new Q(" select Max(Id) as Id from eao_Position ").fetch();
			Integer i = 0;
			if (insertPosition.getRowCount() != 0) {
				i = insertPosition.get(0).getInt("Id") + 1;
			}
			EaoPosition po = new EaoPosition();
			po.setId(i);
			po.setPosition(position);
			po.setType(companyId);
			po.setParentId(Integer.parseInt(parentId));
			transaction.insert(po);
			// 职称修改 人员联动修改
			List<String> list = new ArrayList<>();
			DataTable dataTable = new Q(" select Member from Mh_Resource where Company = ?", companyId).fetch();
			if (dataTable.getRowCount() != 0) {
				for (DataRow dataRow : dataTable) {
					list.add(dataRow.getString(0));
				}
				Q q = new Q(" update eao_is_engineer set IsEngineer = ? where IsEngineer = ?", i, parentId);
				q.and().in("member", list);
				transaction.add(q);
			}
			if (transaction.commit()) {
				s("添加成功");
			} else {
				f("添加失败");
			}
		}

	}

	/**
	 * 点击创建职位时查询职位信息
	 */
	@Priv(loginType = LoginType.Member)
	public void branchList() {
		List<EaoPosition> branchList = new ArrayList<>();
		DataTable branch = new Q(" select * from eao_Position where Id not in (1,2,3,5,6,7,8) and Type = ? ",
				Member.getCompany()).fetch();
		if (branch.getRowCount() != 0) {
			for (DataRow dataRow : branch) {
				EaoPosition po = new EaoPosition();
				po.setId(dataRow.getInt("Id"));
				po.setPosition(dataRow.getString("Position"));
				po.setParentId(dataRow.getInt("ParentId"));
				branchList.add(po);
			}
		}
		List<EaoPosition> branchList1 = new ArrayList<>();
		DataTable branch1 = new Q(" select * from eao_Position where Id in (1,2,3,5,6,7,8) ").fetch();
		if (branch1.getRowCount() != 0) {
			for (DataRow dataRow : branch1) {
				EaoPosition po = new EaoPosition();
				po.setId(dataRow.getInt("Id"));
				po.setPosition(dataRow.getString("Position"));
				po.setParentId(dataRow.getInt("ParentId"));
				branchList1.add(po);
			}
		}
		this.Response.put("newBranch", branchList);
		this.Response.put("oldBranch", branchList1);
	}

	/**
	 * 查询职位信息列表
	 */
	@Priv(loginType = LoginType.Member)
	public void positionList() {
		List<EaoPosition> branchList = new ArrayList<>();
		DataTable branch = new Q(
				" SELECT Id,`Position`,`Type`,ParentId FROM eao_Position where Id not in (SELECT ParentId FROM eao_Position where ParentId is not null) ")
						.fetch();
		if (branch.getRowCount() != 0) {
			for (DataRow dataRow : branch) {
				EaoPosition po = new EaoPosition();
				po.setId(dataRow.getInt("Id"));
				po.setPosition(dataRow.getString("Position"));
				po.setParentId(dataRow.getInt("ParentId"));
				branchList.add(po);
			}
		}
		this.Response.put("branchList", branchList);
	}

	/**
	 * 人员添加
	 */
	@Priv(loginType = LoginType.Member)
	public void addMember() {
		Transaction t = new Transaction();
		String mobile = $V("UserName");
		String memberId = $V("MemberId");
		String build = $V("Build");
		AhjMemberRemark me = new AhjMemberRemark();
		int num = new Q(" SELECT count(*) FROM mh_resource where Company = ? ", Member.getCompany()).executeInt();
		String oldnum = new Q(" SELECT CountMember FROM eao_member_life where MemberId = ? ", Member.getCompany())
				.executeString();
		if (StringUtils.isEmpty(oldnum)) {
			f("请联系管理员设置该一级账号下二级账号数量！");
			return;
		}
		if (num >= Integer.parseInt(oldnum)) {
			f("该一级账号下的二级账号已达到上限，无法添加新的账号");
			return;
		}
		if (StringUtil.isNotEmpty(mobile) && StringUtil.isEmpty(memberId) && StringUtil.isEmpty(build)) {
			DataTable dt = new Q(" SELECT ID FROM ec_member where Mobile = ? ", mobile).fetch();
			if (dt.getRowCount() != 0) {
				EcMember ecMember = new EcMember();
				ecMember.setValue(dt.get(0));
				DataTable company = new Q(" SELECT * FROM mh_resource where Member = ? ", ecMember.getID()).fetch();

				if (company.getRowCount() != 0) {
					boolean flag = false;
					for (DataRow dataRow : company) {
						if (Member.getCompany() == dataRow.getLong("Company")) {
							flag = true;
						}
					}
					if (flag) {
						s("该账号已属于本公司");
						return;
					} else {
						s("该账号已属于其他公司");
						return;
					}
				} else {
					s("该账号是已有账号");
					this.Response.put("user", dt.get(0).toMapx());
					return;
				}
			} else if (dt.getRowCount() == 0) {
				f("该账号未创建");
				return;
			}
		}
		if ("0".equals(build)) {// 创建
			Mapx<String, Object> mdata = new Mapx<String, Object>();
			mdata.put("SiteID", 1);
			mdata.put("Status", "Y");
			mdata.put("Mobile", mobile);
			mdata.put("Password", "123456");
			mdata.put("Type", CommonMemberType.ID);
			mdata.put("Source", "BA");
			mdata.put("AddTime", new Date());
			mdata.put("AddUser", Member.getUID());
			EcMember member = MemberBL.insert(UUID.randomUUID().toString().replace("-", ""), mdata, t);
			me.setID(member.getID());
			
			CrmPeople people = new CrmPeople();
			people.setID(member.getID());
			if (people.fill()) {
				people.setMobile(member.getMobile());
				people.setModifyTime(new Date());
				people.setModifyUser(Member.getRealName());
				t.update(people);
			} else {
				people.setMobile(member.getMobile());
				people.setAddTime(new Date());
				people.setAddUser(Member.getRealName());
				t.insert(people);
			}

			MhResource mr = new MhResource();
			mr.setMember(member.getID());
			mr.setFullName("安环家网友");
			if (mr.fill()) {
				mr.setCompany(Member.getCompany());
				mr.setModifyUser(Long.toString(Member.getUID()));
				mr.setRType("1");
				mr.setModifyTime(new Date());
				t.update(mr);
			} else {
				mr.setCompany(Member.getCompany());
				mr.setAddTime(new Date());
				mr.setRType("1");
				mr.setAddUser(Long.toString(Member.getUID()));
				t.insert(mr);
			}
			
			if (me.fill()) {
				// 修改
				if (StringUtil.isNotEmpty(me.getRemarkMember())) {
					if (!me.getRemarkMember().contains("eao")) {
						me.setRemarkMember(me.getRemarkMember().trim() + "," + "eao");
					}
				} else {
					me.setRemarkMember("eao");
				}
				me.setModifyTime(new Date());
				t.update(me);
			} else {
				// 新增
				me.setRemarkMember("eao");
				me.setAddTime(new Date());
				t.insert(me);
			}
			if (t.commit()) {
				s("人员添加成功");
			} else {
				f("人员添加失败");
			}
		} else if ("1".equals(build)) {// 添加
			DataTable dataTable = new Q(" SELECT ID,UserName FROM ec_member where Mobile = ? ", mobile).fetch();
			if (StringUtil.isNotEmpty(memberId)) {
				Mapx<String, Object> mdata = new Mapx<String, Object>();
				mdata.put("SiteID", 1);
				mdata.put("Status", "Y");
				mdata.put("Mobile", mobile);
				mdata.put("Type", CommonMemberType.ID);
				mdata.put("Source", "BA");
				mdata.put("ModifyTime", new Date());
				mdata.put("ModifyUser", Member.getUID());
				EcMember member = MemberBL.update(dataTable.get(0).getString("UserName"), mdata, t);
				me.setID(member.getID());
				CrmPeople people = new CrmPeople();
				people.setID(member.getID());
				if (people.fill()) {
					people.setFullName(member.getRealName());
					people.setMobile(member.getMobile());
					people.setAddTime(new Date());
					t.update(people);
				} else {
					people.setFullName(member.getRealName());
					people.setMobile(member.getMobile());
					people.setAddTime(new Date());
					t.insert(people);
				}

				MhResource mr = new MhResource();
				mr.setMember(member.getID());
				mr.setCompany(Member.getCompany());
				if (StringUtils.isEmpty(member.getRealName())) {
					mr.setFullName("安环家网友");
				} else {
					mr.setFullName(member.getRealName());
				}
				mr.setAddTime(new Date());
				mr.setRType("1");
				mr.setAddUser(Member.getUID() + "");
				t.insert(mr);
				
				if (me.fill()) {
					// 修改
					if (StringUtil.isNotEmpty(me.getRemarkMember())) {
						if (!me.getRemarkMember().contains("eao")) {
							me.setRemarkMember(me.getRemarkMember().trim() + "," + "eao");
						}
					} else {
						me.setRemarkMember("eao");
					}
					me.setModifyTime(new Date());
					t.update(me);
				} else {
					// 新增
					me.setRemarkMember("eao");
					me.setAddTime(new Date());
					t.insert(me);
				}
				if (t.commit()) {
					s("人员添加成功");
				} else {
					f("人员添加失败");
				}
			}
		}
	}

	/**
	 * 点击创建部门时查询部门信息
	 */
	@Priv(loginType = LoginType.Member)
	public void addBumen() {
		List<EaoBranchVo> bumen = new ArrayList<>();
		DataTable branch = new Q(
				" select br.Id,br.`Name` as Bumen,OldBr.`Name` as Gongsi,oldBr.Id as GingsiId from (select Id,ParentInnerCode,`Name` from Mh_Branch where CompanyType = 2) as br,mh_branch as oldBr where oldBr.Id = br.ParentInnerCode and oldBr.CompanyType in (0,1) ")
						.fetch();
		if (branch.getRowCount() != 0) {
			for (DataRow dataRow : branch) {
				EaoBranchVo br = new EaoBranchVo();
				br.setId(String.valueOf(dataRow.getLong("Id")));
				br.setGongsiId(dataRow.getInt("GingsiId"));
				br.setBumen(dataRow.getString("Bumen"));
				br.setGongsi(dataRow.getString("Gongsi"));
				bumen.add(br);
			}
		}
		List<EaoBranchVo> gongsi = new ArrayList<>();
		DataTable branch1 = new Q(" select Id,`Name` from Mh_Branch where CompanyType in (0,1) ").fetch();
		if (branch1.getRowCount() != 0) {
			for (DataRow dataRow : branch1) {
				EaoBranchVo br = new EaoBranchVo();
				br.setId(String.valueOf(dataRow.getLong("Id")));
				br.setGongsi(dataRow.getString("Name"));
				gongsi.add(br);
			}
		}
		this.Response.put("newBumen", bumen);
		this.Response.put("newGongsi", gongsi);
	}

	/**
	 * 部门添加/修改
	 */
	@Priv(loginType = LoginType.Member)
	public void saveUpdate() {
		String id = $V("Id");
		String name = $V("Bumen");
		String parentId = $V("Gongsi");
		if (StringUtil.isNotEmpty(id)) {
			DataTable branch1 = new Q(" select * from Mh_Branch where Id ", id).fetch();
			if (branch1.getRowCount() != 0) {
				Transaction trans = new Transaction();
				Q q = new Q(" update mh_branch SET  ");
				if (StringUtil.isNotEmpty(name)) {
					q.append("Name = ? ");
					if (StringUtil.isNotEmpty(parentId)) {
						q.append(",");
					}
				}
				if (StringUtil.isNotEmpty(parentId)) {
					q.append("ParentInnerCode = ? ");
				}
				if (StringUtil.isNotEmpty(name)) {
					q.append(" ", name);
				}
				if (StringUtil.isNotEmpty(parentId)) {
					q.append(" ", parentId);
				}
				q.append(" where Id = ? ", id);
				trans.add(q);
				if (trans.commit()) {
					s("修改成功");
				} else {
					f("修改失败");
				}
			} else {
				Transaction transaction = new Transaction();
				MhBranch po = new MhBranch();
				po.setValue(this.Request);
				transaction.insert(po);
				if (transaction.commit()) {
					s("添加成功");
				} else {
					f("添加失败");
				}
			}
		} else {
			f("id为空");
		}

	}

}
